import { setVersion, getVersion } from '../../utils/auth.js'
import store from '@/store/index.js'
export default {
	namespaced: true,
	state: {
		allMes: {}, //所有消息列表统一保存
		captain: [], //战队消息列表
		unread: [], //未读消息列表
		waitMes: {}, //loading中的消息
		loadingIDArr: []
	},
	mutations: {
		// 删除一条消息
		DELETE_MSG(state, id) {
			state.captain = state.captain.filter(item => {
				return item !== id
			})
			delete state.allMes[id]
		},
		//存储列表总消息------在用
		SET_ALLMES(state, data) {
			state.allMes[data['id']] = data;
			state.captain.push(data['id']);
		},
		// 添加---发送中的消息------------在用
		ADD_LOADING(state, data) {
			state.loadingIDArr.unshift(data['id'])
		},
		// 替换其中加载中的数据------在用
		Add_ONE(state, data) {
			// 获取data 中sencmsgcontent 字符串 并JSON.parse() 转对象
			let msgcontent = JSON.parse(data.sencmsgcontent);
			// allMes 已预先存储
			if (msgcontent.hasOwnProperty('ctime') && state.allMes.hasOwnProperty(msgcontent['ctime'])) {
				// state.allMes[msgcontent['ctime']] = data;
				// state.loadingIDArr = state.loadingIDArr.filter(item => item != JSON.parse(data.sencmsgcontent)['ctime'])

				Reflect.deleteProperty(state.allMes, msgcontent['ctime']);
				state.allMes[data['id']] = data;
				let a = state.captain.indexOf(msgcontent['ctime']);
				state.captain.splice(a, 1, data['id']);
			} else { // allMes 未存储
				state.allMes[data['id']] = data;
				state.captain.unshift(data['id']);
			}
			// if (Object.hasOwn(msgcontent, 'ctime') && Object.hasOwn(state.allMes, msgcontent['ctime'])) {
			// 	state.allMes[msgcontent['ctime']] = data;
			// 	state.loadingIDArr = state.loadingIDArr.filter(item => item != JSON.parse(data.sencmsgcontent)['ctime'])
			// }else{ // allMes 未存储
			// 	state.allMes[data['id']] = data;
			// 	state.captain.unshift(data['id']);
			// }
		},
		//添加新消息
		SET_CAPTAIN(state, data) {
			console.log('data-----', data)
			state.captain.unshift(data['id']);
		},
		//强制替换消息记录的某一条
		SET_CAPTAINIDX(state, data) {
			state.captain.splice(data.idx, 1, data.id);
			// 替换完成 删除加载列表里该条本地记录
			Reflect.deleteProperty(state.waitMes, data['id'])
		},
		// 自己发送的信息ID 存储在本地 当服务器回馈的时候进行对比
		SET_WAITMES(state, data) {
			state.waitMes[data['id']] = data;
		},
		//清空未读列表
		SET_MES(state, id) {
			state.unread = [];
		},
		//合并历史消息
		HISTORY_MES(state, data) {
			state.captain = [...state.captain, ...data]
		},
		//清空当前聊天列表
		CLEAR_CAPTAIN(state) {
			state.allMes = {};
			state.unread = [];
			state.captain = [];
		},
		//删除掉总列表某一条数据
		DELETE_ALLMES(state, name) {
			Reflect.deleteProperty(state.allMes, name)
		},
	},
	actions: {

	}
}